package model;

import java.util.Collection;

import persistency.controller.RegistrationController;

import controller.ConfigurationController;

public class RegistrationValidateMaxStudyPoints extends RegistrationValidator{

	private static final int MAX_CREDITS = Integer.parseInt(ConfigurationController.instance().getProperty("registration.maxStudyPoints"));
	
	public RegistrationValidateMaxStudyPoints(RegistrationValidationBehavior registrationValidationBehavior){
		super(registrationValidationBehavior);
	}
	
	@Override
	public void validate(Registration registration) throws Exception {
		super.validate(registration);
		int courseCredits = registration.getOfferedCourse().getCourse().getCredits();
		Collection<Registration> studentRegistrations =  RegistrationController.instance().getByStudentIdRegistrations(registration.getStudent().getStudentNumber());
		int studentTotal = courseCredits;
		for(Registration reg: studentRegistrations){
			studentTotal += reg.getOfferedCourse().getCourse().getCredits();
		}
		if(studentTotal > MAX_CREDITS){
			throw new Exception("Maxcredits limit exceeded.");
		}
		
	}

}
